package com.zany.util.action;


import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;





/**
 * 登录信息session
 * @author chengY
 * @version 1.0
 */

public class SessionInfo {
	
	private static Logger logger = Logger.getLogger(SessionInfo.class);
	private static SessionInfo SessionInfo = new SessionInfo();
	
	/**
     * 得到SessionInfo实例
     * @return SessionInfo  SessionInfo实例
     */
    public static SessionInfo getInstance() {
        return (SessionInfo == null) ? (SessionInfo = new SessionInfo()) : (SessionInfo);
    }
    /**
     * 
     * @param request
     * @param key
     * @return
     */
    private static void setSessionAttribute(HttpServletRequest request, SessionKey key, String value) {
        request.getSession().setAttribute(key.getKey(), value);
    }
    /**
     * 
     * @param helper
     * @param key
     * @return
     * @throws Exception
     */
    private static String getSessionAttributeToStringNotNull(HttpServletRequest request, SessionKey key)
        throws Exception {

        try {
        	return getSessionAttribute(request, key.getKey()).toString();
        }
        catch (NullPointerException npe) {
            logger.error("Session error : user is null!", npe);
            throw new Exception("Session error : user is null!");
        }
    }
    /**
     * 
     * @param helper
     * @param key
     * @return
     */
    private static String getSessionAttribute(HttpServletRequest request, String key) {

        return (String) request.getSession().getAttribute(key);
    }
    /**
     * 
     * Set session's 机构序号.
     * 
     * @param request
     * @param userOrgID   机构序号
     */
    public static void setUserOrg(HttpServletRequest request, String userOrg) {

        setSessionAttribute(request, SessionKey.USER_ORG, userOrg);
    }
    /**
     * 
     * Get session's 当前用户机构号.
     * 
     * @param helper
     * @return  当前用户机构号
     * @throws WebCmdException
     */
    public static String getUserOrg(HttpServletRequest request) throws Exception {

        return getSessionAttributeToStringNotNull(request, SessionKey.USER_ORG);
    }
    /**
     * 
     * Set session's 用户名.
     * 
     * @param request
     * @param userName   用户名
     */
    public static void setUserName(HttpServletRequest request, String userName) {

        setSessionAttribute(request, SessionKey.USER_NAME, userName);
    }
    /**
     * 
     * Get session's 当前用户名.
     * 
     * @param helper
     * @return  当前用户名
     * @throws WebCmdException
     */
    public static String getUserName(HttpServletRequest request) throws Exception {

        return getSessionAttributeToStringNotNull(request, SessionKey.USER_NAME);
    }
    /**
     * 
     * Set session's 角色代码.
     * 
     * @param request
     * @param roleCode   角色代码
     */
    public static void setRoleCode(HttpServletRequest request, String roleCode) {

        setSessionAttribute(request, SessionKey.ROLE_CODE, roleCode);
    }
    /**
     * 
     * Get session's 角色代码.
     * 
     * @param helper
     * @return  角色代码
     * @throws WebCmdException
     */
    public static String getRoleCode(HttpServletRequest request) throws Exception {

        return getSessionAttributeToStringNotNull(request, SessionKey.ROLE_CODE);
    }
    /**
     * 
     * Set session's 用户ID.
     * 
     * @param request
     * @param roleCode   用户ID
     */
    public static void setUserCode(HttpServletRequest request, String userCode) {

        setSessionAttribute(request, SessionKey.USER_CODE, userCode);
    }
    /**
     * 
     * Get session's 用户ID.
     * 
     * @param helper
     * @return  用户ID
     * @throws WebCmdException
     */
    public static String getUserCode(HttpServletRequest request) throws Exception {

    	return getSessionAttributeToStringNotNull(request, SessionKey.USER_CODE);
    }
    private enum SessionKey{
    	/**
    	 * 用户名
    	 */
    	USER_NAME("USER_NAME"),
    	/**
    	 * 角色代码
    	 */
    	ROLE_CODE("ROLE_CODE"),
    	/**
    	 * 用户ID
    	 */
    	USER_CODE("USER_CODE"),
    	/**
    	 * 用户所在机构
    	 */
    	USER_ORG("USER_ORG")
    	;
    	
    	private String key;

        private SessionKey(String key) {
            this.key = key;
        }

        private String getKey() {
            return this.key;
        }
    }
    
    public static void setSessionCateQory(HttpServletRequest request ,String key ,String value){
    	request.getSession().setAttribute(key, value);
    }
    
    public static String getSessionCateQory(HttpServletRequest request, String key) {   	
        return (String)request.getSession().getAttribute(key);
    }

}
